<template>
  <el-dialog
    title="修改密码"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="账号">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="头像" prop="photo">
        <el-upload
          :on-success="handleAvatarSuccess"
          :show-file-list="false"
          action="/api/file/upload"
          class="avatar-uploader">
          <img :src="perPic" class="avatar" id="avatar" v-if="showPic">
          <i class="el-icon-plus avatar-uploader-icon" v-else></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input type="phone" v-model="dataForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="原密码" prop="password">
        <el-input type="password" v-model="dataForm.password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword1">
        <el-input type="password" v-model="dataForm.newpassword1"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newpassword2">
        <el-input type="password" v-model="dataForm.newpassword2"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {clearLoginInfo} from '@/utils'
  import {treeDataTranslate, handleImg} from '@/utils'
  export default {
    data () {
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newpassword1 !== value) {
          callback(new Error('确认密码与新密码不一致'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          password: '',
          newpassword1: '',
          newpassword2: '',
          phone:'',
          photo:''
        },
        showPic: false,
        perPic: '',
        dataRule: {
          password: [
            {required: true, message: '原密码不能为空', trigger: 'blur'}
          ],
          newpassword1: [
            {required: true, message: '新密码不能为空', trigger: 'blur'}
          ],
          newpassword2: [
            {required: true, message: '确认密码不能为空', trigger: 'blur'},
            {validator: validateConfirmPassword, trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      userName: {
        get () {
          return this.$store.state.user.name
        }
      },
      mainTabs: {
        get () {
          return this.$store.state.common.mainTabs
        },
        set (val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      },
      photo: {
        get (){
          return this.$store.state.user.photo
        }
      }
    },
    methods: {
      // 初始化
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.photo = this.photo
          handleImg(this.photo, 'avatar')
          this.dataForm.phone = this.$store.state.user.phone
          this.showPic = true
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              headers: {
                'Content-Type': 'application/json; charset=utf-8'
              },
              url: 'change/myInfo',
              method: 'post',
              data: this.$http.adornData({
                'loginName':this.dataForm.loginName,
                'password': this.dataForm.password,
                'newpassword1': this.dataForm.newpassword1,
                'newpassword2': this.dataForm.newpassword2,
                'photo':this.dataForm.photo,
                'phone':this.dataForm.phone
              }, false)
            }).then(() => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$nextTick(() => {
                    this.mainTabs = []
                    clearLoginInfo()
                    this.$router.replace({name: 'login'})
                  })
                }
              })
            })
          }
        })
      },
      handleAvatarSuccess (res, file) {
        this.perPic = URL.createObjectURL(file.raw);
        this.dataForm.photo = res.data.bucketName+'-'+res.data.fileName
        this.showPic = true
      }
    }
  }
</script>

